package com.hnzq.cloud.freshuserassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.freshuserassembly.domain.FreResource;
import com.hnzq.cloud.freshuserassembly.domain.FreRoleResource;
import com.hnzq.cloud.freshuserassembly.exception.ResourceException;
import com.hnzq.cloud.freshuserassembly.mapper.FreResourceMapper;
import com.hnzq.cloud.freshuserassembly.mapper.FreRoleResourceMapper;
import com.hnzq.cloud.freshuserassembly.service.IFreResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周皓月
 * @since 2022-02-03
 */
@Service
public class FreResourceServiceImpl extends ServiceImpl<FreResourceMapper, FreResource> implements IFreResourceService {

    @Resource
    private FreRoleResourceMapper mapper;

    @Override
    public List<FreResource> getAllResourcesByRoleId(Integer roleId){
        QueryWrapper<FreRoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<FreRoleResource> roleResourceList = mapper.selectList(wrapper);
        if (roleResourceList.size()<1||null==roleResourceList){
            return new ArrayList<FreResource>();
        }
        List<Integer> resourceIds = roleResourceList.stream().map(item -> item.getResourceId()).collect(Collectors.toList());
        List<FreResource> resourceList = this.baseMapper.selectBatchIds(resourceIds);
        return resourceList;
    }

    @Override
    public List<FreResource> getAllPublicResources(){
        QueryWrapper<FreResource> wrapper = new QueryWrapper<>();
        wrapper.eq("resouce_status",0);
        List<FreResource> list = list(wrapper);
        return list;
    }
}
